<template>
<!--  element-ui中表单组件,model与form中的数据绑定-->
  <div style="width: 80%">
    <h2 style="padding-bottom: 20px">新增用户</h2>
    <!--绑定校验规则-->
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm"  label-width="115px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save"><span>提交</span></el-button>
      <el-button type="danger" @click="reset"><span>取消</span></el-button>
    </div>
  </div>

</template>

<script>
import request from "@/utils/request";
export default {
  name: "AddAdmin",
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!/^[1]([3-9])[0-9]{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      }
      callback();
    };
    return {
      form: { },
      rules: {
        name: [
          {required: true, message: '请输入用户名姓名', trigger: 'blur'},
          {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
        ],
        phone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    save() {
      /*elemeny-ui校验模板*/
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.post('/admin/save', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功');
              this.form = ''
            } else {
              this.$notify.error('新增失败');
            }
          })
        }
      })
    },
    reset() {
      this.form = {}
    }
  }
}
</script>

<style scoped>

</style>